<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-11-7
 * Time: 下午1:50
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityTasting {


    /**
     * 得到活动的结束时间
     * @$arg_array 参数对象
     * @return
     * @author  20141111 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activityEndtime(){
        global $G_SHOP;

        $sql_master="SELECT endTime FROM `activity_events` WHERE eventsID='SC'";
        $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master);
        $endTime=null;
        if($temp_item_master){ //是"1元饼干"活动页注册的会员
            $endTime = $temp_item_master[0]->endTime;
        }
        return $endTime;
    }

    /**
     * 得到活动的信息
     * @return $item_arry
     * @author  20141111 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master(){
        global $G_SHOP;

        //<<<<<<<<<<<<<<<<<<<<<<得到试吃申请状态 0：未提交试吃申请，1：已提交试吃申请>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $status=0;
       if($G_SHOP->currentPCustID){
           $sql_master="SELECT COUNT(1) AS item_count FROM `activity_tasting` WHERE pcustID=".$G_SHOP->currentPCustID;
           $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master);
           $item_count = $temp_item_master[0]->item_count;
           if($item_count>0){ //已提交试吃申请
               $status=1;
           }
       }

        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动的结束时间和申请人数>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT endTime,number,sourceID FROM `activity_events` WHERE eventsID='SC'";
        $temp_item_master2 = &$G_SHOP->DBCA->stmt_query($sql_master2);
        $endtime=null;
        $number=7;
        $sourceID="";
        if($temp_item_master2){ //是"1元饼干"活动页注册的会员
            $endtime = $temp_item_master2[0]->endTime;
            $number = $temp_item_master2[0]->number;
            $sourceID= $temp_item_master2[0]->sourceID;
        }

        //用户信息数组
        $customer_array = array(
            'status'	    => 	$status,
            'endtime'	    => 	$endtime,
            'activity_num'	    => 	$number,
            'sourceID'   => 	$sourceID
        );

        return $customer_array;

    }


    /**
     * 保存试吃申请表信息
     * @author  20141111 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function saveTasting($arg_array) {
        global $G_SHOP;

        //检查参数
        if (!isset($arg_array['pcustID'])) {
            return array( 'code' => 400, 'message' => '会员ID不能为空' );
        }

        if (!isset($arg_array['tasting_name'])) {
            return array( 'code' => 400, 'message' => '姓名不能为空' );
        }

        if ($arg_array['tasting_mobile'] != 7) {
            if (!is_mobile($arg_array['tasting_mobile'])) {
                return array( 'code' => 400, 'message' => '请输入手机号码' );
            }
        }

        if (!isset($arg_array['tasting_cityid'])) {
            return array( 'code' => 400, 'message' => '城市不能为空' );
        }

        if (!isset($arg_array['tasting_countyid'])) {
            return array( 'code' => 400, 'message' => '地区不能为空' );
        }

        if (!isset($arg_array['tasting_address'])) {
            return array( 'code' => 400, 'message' => '地址不能为空' );
        }

        //取值
        $custID=$arg_array["custID"]; //会员名
        $pcustID=$arg_array["pcustID"]; //会员ID
        $tasting_name=$arg_array["tasting_name"]; //姓名
        $tasting_mobile=$arg_array["tasting_mobile"]; //手机
        $tasting_province=$arg_array["tasting_province"]; //省份
        $tasting_provinceid=$arg_array["tasting_provinceid"]; //省份ID
        $tasting_city=$arg_array["tasting_city"]; //城市
        $tasting_cityid=$arg_array["tasting_cityid"]; //城市ID
        $tasting_county=$arg_array["tasting_county"]; //地区
        $tasting_countyid=$arg_array["tasting_countyid"]; //地区ID
        $tasting_address=$arg_array["tasting_address"]; //地址
        $tasting_phone=$arg_array["tasting_phone"]; //电话

        try{
            $G_SHOP->DBCA->beginTransaction();

            //保存试吃申请表数据
            $activity_tasting = new entity('activity_tasting');
            $activity_tasting->creator = $custID;
            $activity_tasting->pcustID = $pcustID;
            $activity_tasting->custID = $custID;
            $activity_tasting->tasting_name = $tasting_name;
            $activity_tasting->tasting_mobile = $tasting_mobile;
            $activity_tasting->tasting_province = $tasting_province;
            $activity_tasting->tasting_provinceid = $tasting_provinceid;
            $activity_tasting->tasting_city = $tasting_city;
            $activity_tasting->tasting_cityid = $tasting_cityid;
            $activity_tasting->tasting_county = $tasting_county;
            $activity_tasting->tasting_countyid = $tasting_countyid;
            $activity_tasting->tasting_address = $tasting_address;
            $activity_tasting->tasting_phone = $tasting_phone;
            if (!$G_SHOP->DBCA->save($activity_tasting)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }

            //试吃人数+1
            $activityEvents=new entity('activity_events');
            $activityEvents->eventsID = "SC";
            if($G_SHOP->DBCA->fetch($activityEvents) ) {
                $activityEvents->number += 1;//修改试吃申请人数
            }
            $G_SHOP->DBCA->save($activityEvents) ;
            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message' => '保存成功' );
        }
        catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }


    }


} 